package ait.mediabroadcast.pauseresume;

import java.util.Map;

import org.teleal.cling.controlpoint.SubscriptionCallback;
import org.teleal.cling.model.gena.CancelReason;
import org.teleal.cling.model.gena.GENASubscription;
import org.teleal.cling.model.message.UpnpResponse;
import org.teleal.cling.model.meta.Service;
import org.teleal.cling.model.state.StateVariableValue;

import ait.mediabroadcast.helper.MyConstants;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class PauseResumeSubscriptionCallback  extends SubscriptionCallback {

	private static final String LOGTAG = PauseResumeSubscriptionCallback.class.toString();
	private Handler mHandler; 

	public PauseResumeSubscriptionCallback(Service service, Handler handler) {
		super(service);
		mHandler = handler;
	}

	 @Override
    protected void failed(GENASubscription subscription,
                          UpnpResponse responseStatus,
                          Exception exception,
                          String defaultMsg) {
//        presenter.onConnectionFailure(defaultMsg);
		 Log.e(LOGTAG, "failed subscribing: " + defaultMsg);
    }

    public void established(GENASubscription subscription) {
    	Log.d(LOGTAG, subscription.getActualDurationSeconds() + "");
        
        // presenter.onConnected();
    }

    public void ended(GENASubscription subscription, CancelReason reason, UpnpResponse responseStatus) {
        Log.d(LOGTAG, "Subscription with service ended. " + (reason != null ? "Reason: " + reason : ""));

        // presenter.onDisconnected();
    }

    public void eventReceived(GENASubscription subscription) {
        Log.d(LOGTAG, "Event received, sequence number: " + subscription.getCurrentSequence());
//        Log.d(LOGTAG, "Event received, values: " + )

        Map<String, StateVariableValue> map = subscription.getCurrentValues();
         final StateVariableValue statusValue = map.get("Status");
         final StateVariableValue serverValue = map.get("Server");
         boolean changed = false;
         
         Log.d(LOGTAG, "Status received, values: " + statusValue.getValue() );
		 
		 changed = true;
		 Object [] values = {statusValue, serverValue};
		 Message containerItemMsg = mHandler.obtainMessage(
				 MyConstants.RECEIVED_BROADCAST, (Object) values);
		 containerItemMsg.sendToTarget();
    }

    public void eventsMissed(GENASubscription subscription, int numberOfMissedEvents) {
        Log.w(LOGTAG, "Events missed (" + numberOfMissedEvents + "), consider restarting this control point!");
    }
}